﻿namespace OleDbDAL.Stock
{
    using DBUtility;
    using IDal.Stock;
    using Model.Stock;
    using System;
    using System.Data;
    using System.Text;

    public class StockApp : IStock
    {
        private string dbname = "Systems";

        public void Add(StockEnt model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into T_Stock(");
            builder.Append("DlId,ChainNum,LotNum,ProcurementId,SupplierListId,CommTypeId,CommId,CommName,Amount,AmoutUnit,AmoutMoney,TimeExpired");
            builder.Append(")");
            builder.Append(" values (");
            builder.Append("" + model.DlId + ",");
            builder.Append("" + model.ChainNum + ",");
            builder.Append("'" + model.LotNum + "',");
            builder.Append("" + model.ProcurementId + ",");
            builder.Append("'" + model.SupplierListId + "',");
            builder.Append("'" + model.CommTypeId + "',");
            builder.Append("'" + model.CommId + "',");
            builder.Append("'" + model.CommName + "',");
            builder.Append("" + model.Amount + ",");
            builder.Append("" + model.AmoutUnit + ",");
            builder.Append("" + model.AmoutMoney + ",");
            builder.Append("'" + model.TimeExpired + "'");
            builder.Append(")");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public void Delete(int StockId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from T_Stock ");
            builder.Append(" where StockId=" + StockId);
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public DataTable GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from T_Stock ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by StockId ");
            return DbHelperOledb.Query(this.dbname, builder.ToString());
        }

        public int GetMaxId(string swhere)
        {
            return DbHelperOledb.GetMaxID(this.dbname, "StockId", "T_Stock", swhere);
        }

        public StockEnt GetModel(int StockId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select StockId,DlId,ChainNum,LotNum,ProcurementId,SupplierListId,CommTypeId,CommId,CommName,Amount,AmoutUnit,AmoutMoney,TimeExpired from T_Stock ");
            builder.Append(" where StockId=" + StockId);
            StockEnt ent = new StockEnt();
            DataTable table = DbHelperOledb.Query(this.dbname, builder.ToString());
            ent.StockId = StockId;
            if (table.Rows.Count > 0)
            {
                if (table.Rows[0]["DlId"].ToString() != "")
                {
                    ent.DlId = int.Parse(table.Rows[0]["DlId"].ToString());
                }
                if (table.Rows[0]["ChainNum"].ToString() != "")
                {
                    ent.ChainNum = int.Parse(table.Rows[0]["ChainNum"].ToString());
                }
                ent.LotNum = table.Rows[0]["LotNum"].ToString();
                if (table.Rows[0]["ProcurementId"].ToString() != "")
                {
                    ent.ProcurementId = int.Parse(table.Rows[0]["ProcurementId"].ToString());
                }
                ent.SupplierListId = table.Rows[0]["SupplierListId"].ToString();
                ent.CommTypeId = int.Parse(table.Rows[0]["CommTypeId"].ToString());
                ent.CommId = int.Parse(table.Rows[0]["CommId"].ToString());
                ent.CommName = table.Rows[0]["CommName"].ToString();
                if (table.Rows[0]["Amount"].ToString() != "")
                {
                    ent.Amount = decimal.Parse(table.Rows[0]["Amount"].ToString());
                }
                if (table.Rows[0]["AmoutUnit"].ToString() != "")
                {
                    ent.AmoutUnit = int.Parse(table.Rows[0]["AmoutUnit"].ToString());
                }
                if (table.Rows[0]["AmoutMoney"].ToString() != "")
                {
                    ent.AmoutMoney = decimal.Parse(table.Rows[0]["AmoutMoney"].ToString());
                }
                if (table.Rows[0]["TimeExpired"].ToString() != "")
                {
                    ent.TimeExpired = DateTime.Parse(table.Rows[0]["TimeExpired"].ToString());
                }
                return ent;
            }
            return null;
        }

        public DataTable GetPageList(int PageSize, int PageIndex, string strWhere)
        {
            string orderString = "";
            int pageCount = 0;
            int recordCount = 0;
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from T_Stock ");
            return DbHelperOledb.ExecutePager(this.dbname, PageIndex, PageSize, "", builder.ToString(), strWhere, orderString, out pageCount, out recordCount, "StockId");
        }

        public void Update(StockEnt model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update T_Stock set ");
            builder.Append("DlId=" + model.DlId + ",");
            builder.Append("ChainNum=" + model.ChainNum + ",");
            builder.Append("LotNum='" + model.LotNum + "',");
            builder.Append("ProcurementId=" + model.ProcurementId + ",");
            builder.Append("SupplierListId='" + model.SupplierListId + "',");
            builder.Append("CommTypeId='" + model.CommTypeId + "',");
            builder.Append("CommId='" + model.CommId + "',");
            builder.Append("CommName='" + model.CommName + "',");
            builder.Append("Amount=" + model.Amount + ",");
            builder.Append("AmoutUnit=" + model.AmoutUnit + ",");
            builder.Append("AmoutMoney=" + model.AmoutMoney + ",");
            builder.Append("TimeExpired='" + model.TimeExpired + "'");
            builder.Append(" where StockId=" + model.StockId + "");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }
    }
}

